package com.nit.lab.mapper.system;

import com.nit.lab.dto.system.PermissionDTO;
import com.nit.lab.dto.system.RoleDTO;
import com.nit.lab.entity.system.RoleEntity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface RoleMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT " +
            "role_id, role_name, role_info, role_order, gmt_create " +
            "FROM system_role " +
            "WHERE TRUE " +
            "<if test='name != null'> AND role_name LIKE CONCAT('%', #{name}, '%') </if>" +
            "<if test='describe != null'> AND role_info LIKE CONCAT('%', #{describe}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'> ${sortField} ${sortOrder}, </if>" +
            "role_order DESC, role_id " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    @Results({
            @Result(column = "role_id", property = "key"),
            @Result(column = "role_id", property = "id"),
            @Result(column = "role_name", property = "name"),
            @Result(column = "role_info", property = "describe"),
            @Result(column = "gmt_create", property = "createTime")
    })
    List<RoleDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("name") String name,
            @Param("describe") String describe,
            @Param("sortField") String sorterField,
            @Param("sortOrder") String sorterOrder) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM system_role " +
            "WHERE TRUE " +
            "<if test='name != null'> AND role_name LIKE CONCAT('%', #{name}, '%') </if>" +
            "<if test='describe != null'> AND role_info LIKE CONCAT('%', #{describe}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("name") String name,
            @Param("describe") String describe) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO system_role (role_name, role_info, gmt_create) " +
            "VALUES (#{roleEntity.roleName}, #{roleEntity.roleInfo}, #{roleEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "roleId", keyColumn = "role_id")
    Integer add(@Param("roleEntity") RoleEntity roleEntity) throws Exception;

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("UPDATE system_role " +
            "SET role_name = #{roleEntity.roleName}, " +
            "role_info = #{roleEntity.roleInfo} " +
            "WHERE role_id = #{roleEntity.roleId}")
    Integer update(@Param("roleEntity") RoleEntity roleEntity) throws Exception;


    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Update("DELETE FROM system_role WHERE role_id = #{roleId}")
    Integer remove(@Param("roleId") Long roleId) throws Exception;

    /**
     * @Description: 根据角色ID获取角色信息
     * @Author: SN
     * @Date: 2020/01/30 18:15
     */
    @Select("SELECT role_id, role_name, role_info, gmt_create " +
            "FROM system_role " +
            "WHERE role_id = #{roleId}")
    @Results({
            @Result(column = "role_id", property = "id"),
            @Result(column = "role_name", property = "name"),
            @Result(column = "role_info", property = "describe"),
            @Result(column = "gmt_create", property = "createTime")
    })
    RoleDTO getRoleByRoleId(@Param("roleId") Long roleId) throws Exception;

    /**
     * @Description: 获取角色信息用于选择
     * @Author: SN
     * @Date: 2020/01/30 18:15
     */
    @Select("SELECT role_id, role_name, role_info, gmt_create " +
            "FROM system_role " +
            "WHERE TRUE")
    @Results({
            @Result(column = "role_id", property = "id"),
            @Result(column = "role_name", property = "name"),
            @Result(column = "role_info", property = "describe"),
            @Result(column = "gmt_create", property = "createTime")
    })
    List<RoleDTO> listRole() throws Exception;

    /**
     * @Description: 根据角色ID获取权限信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("SELECT role_id, menu_code, menu_name " +
            "FROM system_permission, system_menu " +
            "WHERE system_permission.menu_id = system_menu.menu_id " +
            "AND role_id = #{roleId} AND menu_status = 0 ")
    @Results({
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "menu_code", property = "permissionId"),
            @Result(column = "menu_name", property = "permissionName")
    })
    List<PermissionDTO> listPermissionByRoleId(@Param("roleId") Long roleId) throws Exception;

    /**
     * @Description: 根据角色ID获取权限信息用于授权树
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("SELECT system_menu.menu_id, menu_code, menu_name, menu_type " +
            "FROM system_permission, system_menu " +
            "WHERE system_permission.menu_id = system_menu.menu_id " +
            "AND role_id = #{roleId} AND menu_status = 0 ")
    @Results({
            @Result(column = "menu_id", property = "roleId"),
            @Result(column = "menu_code", property = "permissionId"),
            @Result(column = "menu_name", property = "permissionName"),
            @Result(column = "menu_type", property = "actionList")
    })
    List<PermissionDTO> listPermissionByRoleId4Tree(@Param("roleId") Long roleId) throws Exception;

    /**
     * @Description: 获取全部权限信息
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select("SELECT " +
            "menu_id, menu_code, menu_name, menu_father, menu_type " +
            "FROM system_menu " +
            "WHERE menu_status = 0 ORDER BY menu_id ASC")
    @Results({
            @Result(column = "menu_id", property = "roleId"),
            @Result(column = "menu_code", property = "permissionId"),
            @Result(column = "menu_name", property = "permissionName"),
            @Result(column = "menu_father", property = "actions"),
            @Result(column = "menu_type", property = "actionList")
    })
    List<PermissionDTO> listPermission() throws Exception;

    /**
     * @Description: 角色授权(先删除)
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM system_permission " +
            "WHERE role_id = #{roleId} ")
    Integer removeAuthorization(@Param("roleId") Long roleId) throws Exception;

    /**
     * @Description: 角色授权(后插入)
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Insert("INSERT INTO system_permission (role_id, menu_id, gmt_create) " +
            "VALUES (#{roleId}, #{menuId}, #{gmtCreate})")
    @Options(useGeneratedKeys = true, keyColumn = "permission_id")
    Integer addAuthorization(@Param("roleId") Long roleId,
                             @Param("menuId") Long menuId,
                             @Param("gmtCreate") Date gmtCreate) throws Exception;

    /**
     * @description 更新排序字段
     * @author SYD
     * @date 2021/9/6
     */
    @Update("UPDATE system_role " +
            "SET role_order = #{order} " +
            "WHERE role_id = #{id}")
    Integer updateOrder(@Param("id") Long id, @Param("order") Integer order);
}
